//
//  ViewController.swift
//  NewJSQ
//
//  Created by shenboren on 2017/9/25.
//  Copyright © 2017年 shenboren. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var clean: UIButton!
    @IBOutlet weak var div: UIButton!
    @IBOutlet weak var mul: UIButton!
    @IBOutlet weak var add: UIButton!
    @IBOutlet weak var sub: UIButton!
    @IBOutlet weak var num0: UIButton!
    @IBOutlet weak var num1: UIButton!
    @IBOutlet weak var num2: UIButton!
    @IBOutlet weak var num3: UIButton!
    @IBOutlet weak var num4: UIButton!
    @IBOutlet weak var num5: UIButton!
    @IBOutlet weak var num6: UIButton!
    @IBOutlet weak var num7: UIButton!
    @IBOutlet weak var num8: UIButton!
    @IBOutlet weak var num9: UIButton!
    
    @IBOutlet weak var lableResult: UILabel!
    

    var firstOperand: Double = 0.0
    var secondOperand: Double = 0.0
    var isSecond: Bool = false
    var operatorFlag: String = ""
    
    
    
    @IBAction func buttonTap(sender: UIButton) {
        if lableResult.text == "0" || (isSecond && secondOperand == 0.0) {
            
            lableResult.text = ""
        }
        lableResult.text = lableResult.text! + sender.titleLabel!.text!
        
        if isSecond {
            secondOperand = NSString(string: lableResult.text!).doubleValue
        }else {
            firstOperand = NSString(string: lableResult.text!).doubleValue
        }
    }
    
    
    @IBAction func operatorTap(sender: UIButton) {
        if firstOperand != 0 {
            isSecond = true
            switch sender.titleLabel!.text! {
            case "+":
                operatorFlag = "+"
            case "-":
                operatorFlag = "-"
            case "*":
                operatorFlag = "*"
            case "÷":
                operatorFlag = "/"
            default:
                operatorFlag = " "
            }
        }
    }
    
    
    @IBAction func resultTap(sender: UIButton) {
        if isSecond {
            if operatorFlag == "/" && secondOperand == 0 {
                print("Error: 除数不能为0")
                return
            }
            
            var result: Double = 0.0
            switch operatorFlag {
            case "+":
                result = firstOperand + secondOperand
            case "-":
                result = firstOperand - secondOperand
            case  "*":
                result = firstOperand * secondOperand
            case  "/":
                result = firstOperand / secondOperand
            default :
                result = 0.0
            }
            
            lableResult.text = result.description
            
        }
        
    }
    
    @IBAction func clear(sender: UIButton) {
        
        lableResult.text = "0"
        firstOperand = 0.0
        secondOperand = 0.0
        isSecond = false
        operatorFlag = ""
        
    }
  
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

